/*
 * @Author: moeWang
 * @Date: 2022-02-20 19:30:13
 * @LastEditTime: 2022-03-02 17:46:43
 * @LastEditors: moeWang
 * @Description:
 * @FilePath: \nest-web\src\logical\auth\jwt.strategy.ts
 */
// src/logical/auth/jwt.strategy.ts
import { ExtractJwt, Strategy } from 'passport-jwt';
import { PassportStrategy } from '@nestjs/passport';
import { Injectable } from '@nestjs/common';
import { jwtConstants } from './constants';

@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
  constructor() {
    super({
      jwtFromRequest: ExtractJwt.fromHeader('authorization'),
      ignoreExpiration: false,
      passReqToCallback: true,
      secretOrKey: jwtConstants.secret,
    });
  }

  // JWT验证 - Step 4: 被守卫调用
  async validate(req: Request, payload: any) {
    console.log(`JWT验证 - Step 4: 被守卫调用`);
    console.log(payload);
    return payload;
  }
}
